<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">



        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>抵扣登记单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>工程名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=piao_kou','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=piao_kou','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="shimoney" name="shimoney" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled moneyinput">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">已开具销项税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="xiaofee" name="xiaofee" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled moneyinput">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">上期累计进项抵扣金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="wangmoney" name="wangmoney" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled moneyinput">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>本次进项税抵扣合计(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" placeholder="无需填写，自动合计明细中的“税额”" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>认证日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="rendt" id="rendt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">累计进项抵扣含本次(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="leimoney" name="leimoney" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled moneyinput">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联流程</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                            <div class="choose_div" id="flowrelas" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="advanersDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预审人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="advaners"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="nexterDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="nexter"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>发票明细</span></div>
                        <div>
                            <button type="button" onclick="choose_piao_put_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从成本票明细选择</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>发票号码</th>
                            <th>销售方名称</th>
                            <th>开票日期</th>
                            <th>发票类型</th>
                            <th>不含税金额(元)</th>
                            <th>税率(%)</th>
                            <th>税额(元)</th>
                            <th>价税合计(元)</th>
                            <th>已抵扣(元)</th>
                            <th>剩余可抵扣(元)</th>
                            <th>本次抵扣(元)</th>
                            <th>附件</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror mingerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    var param_isturn = getQueryVariable('isturn') || 0;
    if(param_isturn > 0){
        $("#billzhan").remove();
        $("#billtutn").remove();
        $("#billsub").attr('isshen', 1);
    }
    var param_billyushen = getQueryVariable('billyushen') || 0;
    if(param_billyushen == 2){
        $("#billyushen").addClass('layui-hide');
    }

    $("#commentForm").validate({
        rules: {
            rendt: {required: true},
        },
        messages:{
            rendt: {required: "请选择"},
        }
    });

    function choose_piao_put_list(obj){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择工程名称");return false;
        }
        openPage('../public/choose_piao_put_list.php?page=piao_kou'+'&enginid='+enginid,'80%','80%');
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_edit_piao_kou, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                    if(data.advaners_info.length > 0){
                        tan_tags_init('#advaners', data.advaners_info);
                    }
                }
                if(data.isnext){
                    $("#nexterDiv").removeClass('layui-hide');
                    if(data.nexter_info.length > 0){
                        tan_tags_init('#nexter', data.nexter_info);
                    }
                }
                if(data.flowrelas_info.length > 0){
                    set_choose_btn_more('flowrelas',data.flowrelas_info);
                }

                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                    $("#shimoney").val(res.data.shimoney);
                }

                $("#xiaofee").val(res.data.xiaofee);
                $("#wangmoney").val(res.data.wangmoney);
                $("#leimoney").val(res.data.leimoney);
                $("#number").val(data.number);
                $("#money").val(data.money);
                $("#rendt").val(data.rendt);
                $("#explain").val(data.explain);



                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<input type="text" readonly key="0" class="layui-input nums disabled">';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" class="layui-input disabled gonger" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input disabled dt" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input disabled piaotype" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input disabled fee" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input disabled rate" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input disabled ratefee" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input disabled money" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input disabled kou_ratefee" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input disabled ke_ratefee" readonly></td>';
                listAddStr += '<td><input type="number" class="layui-input ben_ratefee"></td>';
                listAddStr += '<td>'+public_show_file_deta('list_list', [],0)+'</td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr,1);

                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td>';
                        str += '<input type="text" value="'+list[key].nums+'" key="'+list[key].listid+'" readonly class="layui-input nums disabled">';
                        str += '</td>';
                        str += '<td><input type="text" class="layui-input disabled gonger" value="'+list[key].gonger+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input disabled dt" value="'+list[key].dt+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input disabled piaotype" value="'+list[key].piaotype_name+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input disabled fee" value="'+list[key].fee+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input disabled rate" value="'+list[key].rate+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input disabled ratefee" value="'+list[key].ratefee+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input disabled money" value="'+list[key].money+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input disabled kou_ratefee" value="'+list[key].kou_ratefee+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input disabled ke_ratefee" value="'+list[key].ke_ratefee+'" readonly></td>';
                        str += '<td><input type="number" class="layui-input ben_ratefee" value="'+list[key].ben_ratefee+'"></td>';
                        str += '<td>'+public_show_file_deta('list_list', list[key].filearr, 0)+'</td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }else{
                    $("#list_list").html(listAddStr);
                }







                update_refresh_row_func()








                initParseMoney();
                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });


        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.isshen = $(this).attr('isshen') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            data.field.enginid = get_choose_val('enginid');
            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }
                if(!validRes){return false;}
            }

            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var listid = $(this).find('.nums').attr('key');
                    var ratefee = $(this).find('.ben_ratefee').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;

                    if(!listid){
                        throw new Error('第'+index+'行，请选择发票信息');
                    }
                    if(!ratefee){
                        throw new Error('第'+index+'行，请输入本次抵扣金额');
                    }

                    list.push({
                        listid:listid,
                        ratefee:ratefee,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".mingerror").find('span').text(e.message);
                $(".mingerror").show();
                return false;
            }
            $(".mingerror").hide();
            data.field.list = list;

            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            delete data.field.shimoney;

            public_flow_submit(data.field, ask_flowdata_add_piao_kou, ask_flowdata_edit_piao_kou, $(this).attr('but'));
            return false;
        });

    });

    function choose_enginid_delete() {
        $("#shimoney").val('');
        $("#xiaofee").val('');
        $("#wangmoney").val('');
        $("#money").val('');
        $("#leimoney").val('');
        $("#list_list").html('');
        $("#pay_list").html('');
    }





    function jisuan_money_all() {
        var allmoney = 0;

        $('#list_list tr').each(function (i) {
            var ratefee = parseValidNum($(this).find('.ben_ratefee').val()) || 0;
            allmoney = parseFloat(allmoney) + parseFloat(ratefee);
        });

        var wangmoney = parseValidNum($(this).find('.wangmoney').val()) || 0;
        var leimoney = parseFloat(allmoney) + parseFloat(wangmoney);

        $("#money").val(parseFormatNum(allmoney,2));
        $("#leimoney").val(parseFormatNum(leimoney,2));
    }


    function update_refresh_row_func() {
        render_date();
        render_from();
        jisuan_money_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }
</script>
</body>
</html>
